package com.wang.back.controller;

import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wang.common.controller.BaseController;
import com.wang.common.pojo.ApiResponse;
import com.wang.common.util.ConstatFinalUtil;
import com.wang.common.util.PageInfoUtil;
import com.wang.users.pojo.ABodyside;
import com.wang.users.pojo.AUsers;
import com.wang.users.service.IABodysideService;
import com.wang.users.service.IUsersDbService;

/**
 * 体侧历史表
 * @author MyJjh
 *
 */
@Controller
@RequestMapping("/back/side")
public class ABodySideController extends BaseController
{

	@Autowired
	private IABodysideService bodysideService;
	@Autowired
	private IUsersDbService usersDbService;
	
	
	/**
	 * 体侧历史列表页面
	 * 
	 * @return
	 * @throws IOException 
	 * @throws ServletException 
	 */
	@RequestMapping("/sidesList")
	public String sidesList(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException
	{
		ConstatFinalUtil.SYS_LOGGER.info("--sidesList--");
		/* 分页信息 */
		PageInfoUtil pageInfoUtil = this.proccedPageInfo(request);
		/* 搜索条件,生成公共的搜索条件 */
		Map<String, Object> condMap = this.proccedSearch(request);
		/*
		 * 查询数据库 所有的数据以JSON的形式返回
		 */
		ApiResponse<ABodyside> apiResponse = this.bodysideService.findCondListService(pageInfoUtil, condMap);
		/* 将结果存储到Request中 */
		request.setAttribute("response", apiResponse.toJSON());
		
		return "/back/sides/sidesList" ;
	}

	/**
	 * 打开添加体侧历史页面
	 * 
	 * @return
	 * @throws IOException 
	 * @throws ServletException 
	 */
	@RequestMapping("/sidesInsert")
	public String sidesInsert(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException
	{
		ConstatFinalUtil.SYS_LOGGER.info("--sidesInsert--");
		Map<String, Object> condMap = new HashMap<String, Object>();
		return "/back/sides/sidesInsert" ;
	}
	

	/**
	 * 添加体侧历史提交操作
	 * 
	 * @return
	 */
	@RequestMapping(value = "sidesInsertSubmit",produces = "text/html;charset=UTF-8" )
	@ResponseBody
	public String sidesInsertSubmit(HttpServletRequest request,HttpServletResponse response,
			ABodyside sides,String email)throws ServletException, IOException
	{
		ConstatFinalUtil.SYS_LOGGER.info("--sidesInsertSubmit--");
		
		Map<String,Object> condMap = new HashMap<String,Object>();
		condMap.put("email", email);
		
		ApiResponse<AUsers> apiResponse = this.usersDbService.findOneUsersService(condMap);
		
		 AUsers users = apiResponse.getDataOneJava();
		 int usersId = users.getId();
		 sides.setUsersId(usersId);
		
		/* 保存结果 */
		ApiResponse<Object> dbResponse = this.bodysideService.saveOneBodysideService(sides);
		ConstatFinalUtil.SYS_LOGGER.info("结果:{}", dbResponse.toJSON().toJSONString());
		String result =  dbResponse.toJSON().toJSONString();
		
		return result;
	}
	
	

	/**
	 * 打开更新体侧历史页面
	 * 
	 * @return
	 * @throws IOException 
	 * @throws ServletException 
	 */
	@RequestMapping("/sidesUpdate")
	public String sidesUpdate(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException
	{
		// operType=update&idType=session
		ConstatFinalUtil.SYS_LOGGER.info("--sidesUpdate--");
		String id = request.getParameter("id");
		String operType = request.getParameter("operType");
		
		/* 准备查询条件 */
		Map<String, Object> condMap = new HashMap<String, Object>();
		condMap.put("id", id);
		/* 查询数据库 */
		ApiResponse<ABodyside> apiResponse = this.bodysideService.findOneBodysideService(condMap);
		/* 存储request */
		request.setAttribute("response", apiResponse.toJSON());
		request.setAttribute("operType", operType);
		
		if ("update".equalsIgnoreCase(operType))
		{
			return "/back/sides/sidesUpdate";
		}else if("stands".equalsIgnoreCase(operType))
		{
			return "/back/sides/standsInfo" ;
		}else if("users".equalsIgnoreCase(operType))
		{
			return "/back/sides/usersInfo" ;
		}
		return "/back/sides/sidesInfo";
	}

	/**
	 * 更新体侧历史提交操作
	 * 
	 * @return
	 */
	@RequestMapping(value = "sidesUpdateSubmit",produces = "text/html;charset=UTF-8" )
	@ResponseBody
	public String sidesUpdateSubmit(HttpServletRequest request,HttpServletResponse response)throws ServletException, IOException
	{
		ConstatFinalUtil.SYS_LOGGER.info("--sidesUpdateSubmit--");
		/* 数据库操作 */
		HttpSession session = request.getSession();
		ApiResponse dbResponse = new ApiResponse<>();
		String id = request.getParameter("id");
		String operType = request.getParameter("operType");

		/* 准备查询条件 */
		Map<String, Object> condMap = new HashMap<String, Object>();
		condMap.put("id", id);
		/* 查询数据库 */
		ApiResponse<ABodyside> apiResponse = this.bodysideService.findOneBodysideService(condMap);
		/* 获取java对象 */
		ABodyside sides = apiResponse.getDataOneJava();
		boolean flag = false;
		if ("update".equalsIgnoreCase(operType))
		{
			/* 接收参数 */
			String vitalcapacity = request.getParameter("vitalcapacity");
			String height = request.getParameter("height");
			String weight = request.getParameter("weight");
			String vitalcapacityFraction = request.getParameter("vitalcapacityFraction");
			String longrunFraction = request.getParameter("longrunFraction");
			String shortrunFraction = request.getParameter("shortrunFraction");
			String bmlFraction = request.getParameter("bmlFraction");
			String abdominalcurlFraction = request.getParameter("abdominalcurlFraction");
			String sittingforwardFraction = request.getParameter("sittingforwardFraction");
			String status = request.getParameter("status");
			
			sides.setVitalcapacity(Integer.valueOf(vitalcapacity));
			sides.setHeight(Double.valueOf(height));
			sides.setWeight(Double.valueOf(weight));
			sides.setVitalcapacityFraction(Double.valueOf(vitalcapacityFraction));
			sides.setLongrunFraction(Double.valueOf(longrunFraction));
			sides.setShortrunFraction(Double.valueOf(shortrunFraction));
			sides.setBmlFraction(Double.valueOf(bmlFraction));
			sides.setAbdominalcurlFraction(Double.valueOf(abdominalcurlFraction));
			sides.setSittingforwardFraction(Double.valueOf(sittingforwardFraction));
			sides.setStatus(Byte.valueOf(status));
			flag = true;
		}
		dbResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(dbResponse.getCode() + ""), Collections.EMPTY_MAP);
		if (flag)
		{
			dbResponse = bodysideService.updateOneBodysideService(sides);
		}
		ConstatFinalUtil.SYS_LOGGER.info("--用户更新返回结果:{}--", dbResponse.toJSON());
		String result =  dbResponse.toJSON().toJSONString();
		return result;
	}

}
